package com.xszx.servlet.user.order;

import com.xszx.bean.Order;
import com.xszx.bean.dto.OrderDto;
import com.xszx.dao.OrderDao;
import com.xszx.dao.UserDao;
import com.xszx.dao.impl.OrderDaoImpl;
import com.xszx.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/GetOrdersServlet")
public class GetOrdersServlet extends HttpServlet {
    static OrderDao orderDao = new OrderDaoImpl();
    static UserDao userDao = new UserDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String username = req.getSession().getAttribute("username").toString();
        if (username == null || username.equals("tourist")) {
            req.setAttribute("msg", "请先登录");
            return;
        }
        Integer userId = userDao.getUserIdByUsername(username);

        List<OrderDto> orders = orderDao.getOrdersByUserId(userId);
        req.setAttribute("orders", orders);
        req.getRequestDispatcher("user/my-order.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
